package cn.org.xwb.bookmall.user.controller;

import cn.org.xwb.bookmall.model.constant.AccountConst;
import cn.org.xwb.bookmall.model.properties.MinioProp;
import cn.org.xwb.bookmall.user.annotation.IsLoginParam;
import cn.org.xwb.bookmall.user.utils.Convert2DtoUtil;
import cn.org.xwb.common.encrypt.annotation.des.DesEn;
import cn.org.xwb.common.encrypt.annotation.rsa.RsaDe;
import cn.org.xwb.common.encrypt.annotation.rsa.RsaDeObj;
import cn.org.xwb.common.encrypt.annotation.rsa.RsaDeParam;
import cn.org.xwb.common.encrypt.constant.EncryptConstant;
import cn.org.xwb.common.file.service.FileUploadService;
import cn.org.xwb.common.file.vo.FileSliceUploadVo;
import cn.org.xwb.common.model.vo.RespResult;
import cn.org.xwb.common.valid.annotation.str.StrNotNull;
import cn.org.xwb.common.valid.annotation.str.StrNotNullObj;
import cn.org.xwb.common.valid.annotation.str.StrNotNullParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/file/upload")
public class FileUploadController {

    @Autowired
    private FileUploadService fileUploadService;

    @Autowired
    private Convert2DtoUtil convert2DtoUtil;

    @Autowired
    private MinioProp minioProp;

    /**
     * 文件分片上传
     *
     * @param fileSliceUploadVo
     * @param desKey
     * @return
     */
    @PostMapping("/slice")
    @StrNotNull
    @RsaDe
    @IsLoginParam
    @DesEn
    public RespResult<Object> fileSliceUpload(
            @RequestParam(AccountConst.ACCOUNT_PARAM_NAME) @StrNotNullParam @RsaDeParam
            String account,
            @StrNotNullObj @RsaDeObj
            FileSliceUploadVo fileSliceUploadVo,
            @RequestParam(EncryptConstant.DES_KEY_PARAM_NAME)
            @StrNotNullParam @RsaDeParam
            String desKey

    ) {
        return fileUploadService.fileSliceUpload(convert2DtoUtil.genFileSliceUploadDto(fileSliceUploadVo));
    }

    /**
     * 获取下一个需要上传的文件分片索引
     *
     * @param filename
     * @param fileType
     * @param md5
     * @return
     */
    @PostMapping("/slice/next")
    @StrNotNull
    @RsaDe
    @IsLoginParam
    @DesEn
    RespResult<Object> getNextFileSliceIdx(
            @RequestParam(AccountConst.ACCOUNT_PARAM_NAME) @StrNotNullParam @RsaDeParam
            String account,
            @RequestParam("filename") @StrNotNullParam @RsaDeParam
            String filename,
            @RequestParam("fileType") @StrNotNullParam @RsaDeParam
            String fileType,
            @RequestParam("md5") @StrNotNullParam @RsaDeParam
            String md5,
            @RequestParam("fileSliceLen")
            Integer fileSliceLen,
            @RequestParam(EncryptConstant.DES_KEY_PARAM_NAME) @StrNotNullParam @RsaDeParam
            String desKey
    ) {
        return fileUploadService.getNextFileSliceIdx(minioProp.getBucket(), filename, fileType, md5, minioProp.getTempDir(), fileSliceLen);
    }

}
